<template>
  <div class="">
    <Table :loading="loading" :form="form" :table="table" @current="current" @selection="selection" @row="row">
      <template #form>
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>

        <el-form-item label="时间">
          <el-date-picker
            v-model="form.Time"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="timeChange"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="Search">查询</el-button>
          <el-button type="primary" @click="dialog = true">添加</el-button>
        </el-form-item>
      </template>
      <template #table>
        <el-table-column type="selection" width="55" align="center" header-align="center"> </el-table-column>
        <el-table-column v-for="(item, index) in tableLabel" :key="index" :prop="item.value" :label="item.label" align="center" header-align="center">
        </el-table-column>
      </template>
    </Table>
    <el-dialog title="" :visible.sync="dialog">
      <el-form :model="dialogForm" label-width="120px">
        <el-form-item label="结算日期："></el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormSub" class="save">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dialog: false,
      loading: false,
      form: {
        Time: [],
        PageIndex: 1,
        PageSize: 100,
        Count: null,
      },
      table: [],
      tableLabel: [
        { value: "title", label: "标题" },
        { value: "tag", label: "标签" },
      ],
      dialogForm: {},
    };
  },
  methods: {
    getList() {
      this.loading = true;
      this.$api.get("", { params: this.form }).then(({ data }) => {
        if (data.Code == 500) this.$message.error("数据异常");
        else {
          let { PageIndex, PageSize, Count, List } = data;

          this.form.PageIndex = PageIndex;
          this.form.PageSize = PageSize;
          this.form.Count = Count;
          this.table = List.map(v => {
            let timer = v.createdAt.split(/[T.]/);
            v.createdAt1 = timer[0] + " " + timer[1];
            return v;
          });
        }
        this.loading = false;
      });
    },
    dialogFormSub() {
      this.$api.post("", this.dialogForm).then(({ data }) => {
        if (data.Code == 200) {
          this.$message.success(data.Msg);
          this.getList();
          this.dialogForm = {};
          this.dialog = false;
        } else this.$message.error(data.Msg);
      });
    },
    Search() {
      this.form.PageIndex = 1;
      this.getList();
    },
    current({ value, Page }) {
      this.form[Page] = value;
      this.getList();
    },
    timeChange(val) {
      this.form.StartTime = val[0];
      this.form.EndTime = val[1];
    },
    selection() {},
    row() {},
  },
  mounted() {
    this.getList();
  },
};
</script>
